/**
 * Created by lvzj on 15/12/3.
 */

$('.contentEmpty').hide();

var topicId = params.tid;
var token = params.token;
var deviceId = params.deviceId;
var title = "";
//var isMore =true;//判断是否有更多数据
var isfinish = true;//isfinish判断每次数据是否加载完成，完成了才可进行下一次加载
var type = "all";
var allTs = 0;
var aroundTs = 0;

function init() {
}
var topicImage;
$.get(serverUrl + '/rest/v1.0/mobile/shares/getTopic/' + topicId, function (json) {
    topicImage = json.data.topicImage;
    title = json.data.title;
    $('.title').html('#' + json.data.title + '#');
    $('.sub').html(json.data.subTitle);
    $('.time').html(json.data.dateCreated.substring(0, json.data.dateCreated.indexOf('T')));
    $('.dec').html('<pre>' + json.data.desc + '</pre>');
    $('#browserCount').html(json.data.browserCount);
    $('#topicImage').attr('src', json.data.topicImage);

    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        var shareData = {
            title: json.data.title,
            desc: json.data.desc,
            imgUrl: json.data.topicImage,
            link: window.location.href
        };

        wx.ready(function () {
            wx.onMenuShareAppMessage({
                title: shareData.title,
                link: shareData.link,
                imgUrl: shareData.imgUrl,
                desc: shareData.desc,
                success: function () {
                }
            });
            wx.onMenuShareTimeline({
                title: shareData.title,
                link: shareData.link,
                imgUrl: shareData.imgUrl,
                success: function () {
                    //showResult();
                }
            });
            wx.onMenuShareQQ(shareData);
            wx.onMenuShareWeibo(shareData);
        });

        $.post('http://weixin-66xiaoqu.babytree.com/wxJs/getWxJsConfig.do', function (json) {
            timestamp = json.data.timestamp;
            appId = json.data.appId;
            nonceStr = json.data.nonceStr;
            signature = json.data.signature;
            shareT(appId, timestamp, nonceStr, signature);
        }, 'json');

        function shareT(appId, timestamp, nonceStr, signature) {
            wx.config({
                debug: false,
                appId: appId,
                timestamp: timestamp,      //生成签名的时间戳
                nonceStr: nonceStr,       //生成签名的随机串
                signature: signature,     //签名
                jsApiList: [
                    'checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo'
                ]
            });
        }
    }

}, 'json');
getContentList(allTs, 'all');

$('#topicImage').click(function () {
    showImage(topicImage, 0);
});

function getContentList(ts, type) {
    $.get(serverUrl + '/rest/v1.0/mobile/shares/getShareByTopicId/' + ts + '/' + topicId, function (json) {

        if (json.data == null)return;

        var str = "";
        if (type == 'all') {
            allTs = json.data.nextTs == null ? 1 : json.data.nextTs;
        } else {
            aroundTs = json.data.nextTs == null ? 1 : json.data.nextTs;
        }

        var list = json.data.list;

        for (var i = 0; i < list.length; i++) {
            var question = list[i];
            str += '<li class="sub-con">';
            str += '<img src="' + question.headImg + '" onclick="toPeronalPage(\'' + question.createdBy.objId + '\')" class="avatar">';
            str += '<div class="userinfo">';
            str += '<p>' + question.createdBy.nickName + '</p>';
            str += '<span class="time">' + ft(question.dateCreated) + '</span>&nbsp;<span class="address">' + question.community.communityName + '</span>';
            str += '</div>';
            str += '<p>';
            str += '<a href="javascript:">' + (question.desc.length > 60 ? (question.desc.substring(0, 60) + "...") : question.desc) + '</a>';
            str += '<div class="clearfix pic-box">';
            var imageList = new Array();
            if (question.images != null) {
                for (var j = 0; j < question.images.length; j++) {
                    var imageUrl = question.images[j].imageURL;
                    imageList.push(imageUrl);
                    str += '<img onclick="showImage(\'' + imageList + '\',\'' + i + '\')" src="' + imageUrl + '" >';
                }
            }
            str += '</div>';
            str += '</p>';
            var zanDesc = question.praiseCount == 0 ? '赞' : question.praiseCount;
            var pinglun = "";
            if (question.replies == null || question.replies.length == 0) {
                pinglun = "评论";
            } else {
                pinglun = question.replies.length;
            }

            str += '</li>';
        }

        $('.topic-part2').html($('.topic-part2').html() + str);
        if ($('.topic-part2').html() == "") {
            $('.contentEmpty').show();
        } else {
            $('.contentEmpty').hide();
        }

        isfinish = true;
    }, 'json');
}

//滑动加载数据
$('#wrapper').scroll(function () {
    var contentHeight = $("#wrapper")[0].scrollHeight;
    var h = $("#wrapper").height();
    if ((contentHeight - h - $(this).scrollTop()) < 60) {
        //$(".loading-tips").show().html('<img src="http://66xiaoqu.babytreeimg.com/images/topic/load.gif">加载中...');
        if (isfinish == true) {
            isfinish = false;
            if (type == 'all' && allTs != 1) {
                getContentList(allTs, 'all');
            } else if (type == 'around' && aroundTs != 1) {
                getContentList(aroundTs, 'around');
            } else {
                isfinish = true;
            }
        }
    }
})
;
